package net.sahv.bdyz.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.ShoppingCartService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

@Controller
@RequestMapping("api/shoppingcart/")
public class ShoppingCartApiController {
	@Autowired
	private ShoppingCartService shoppingCartService;

	@Autowired
	private MemberService memberService;
	@Autowired 
	CookieLocaleResolver resolver;
	

	/**
	 * 查看购物车（查询所有）
	 * 
	 * @author 薛国计
	 * @date 2016年8月5日
	 * @param request
	 * @param pageInfo
	 * @return
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	@RequestMapping(value = "viewshoppingcart", method = RequestMethod.GET)
	public String viewshoppingcart(HttpServletRequest request, PageInfo pageInfo, Model model,HttpServletResponse response) throws FileNotFoundException, IOException {
		// Member member = (Member) request.getSession().getAttribute("member");
		
		Map<String,String> mapUrls=LoadCfgUtil.loadCfg();
		String imgParent=mapUrls.get("targetUrl.img");
		
		Object languageObj=request.getSession().getAttribute("language");
		 String yuyan="";
			if(languageObj!=null){
				String language=String.valueOf(languageObj);
				if(language!=null&&!language.equals("")){
					language=language.toLowerCase();
					if(language.equals("zh_cn")){
						yuyan="ch";
						resolver.setLocale(request, response, Locale.CHINA );
					}else if(language.equals("en")){
						yuyan="en";
						resolver.setLocale(request, response, Locale.ENGLISH );
					}else{
						yuyan="ch";
						resolver.setLocale(request, response, Locale.CHINA );
					}
				}
			}else{
				yuyan="ch";
			}
		Member member = memberService.getCurrent();//获取当前登录人信息
		if (member != null) {
			List<Map<String,Object>> list = shoppingCartService.selectAllByMid(member.getmId(),yuyan);
			for(Map<String,Object> listOne:list){
				String SC_PATTERN_IMG=(String) listOne.get("SC_PATTERN_IMG");
				listOne.remove("SC_PATTERN_IMG");
				listOne.put("SC_PATTERN_IMG", imgParent+SC_PATTERN_IMG);
			}
			//品图片路径
			model.addAttribute("list", list);
		} else {
			model.addAttribute("list", "");
		}
		return "phone/userCenter/shoppingCart";

	}

}
